package com.portal.controller;

import com.portal.common.Result;
import com.portal.entity.Footer;
import com.portal.service.FooterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通用数据Controller
 */
@Api(tags = "通用数据接口")
@RestController
@RequestMapping("/common")
public class CommonController {
    
    @Autowired
    private FooterService footerService;
    
    /**
     * 获取页脚导航数据
     */
    @ApiOperation("获取页脚导航数据")
    @GetMapping("/footer")
    public Result<Map<String, Object>> getFooterData() {
        try {
            // 获取已启用的页脚导航数据
            List<Footer> footerList = footerService.getEnabledFooterList();
            
            // 按类型分组
            Map<Integer, List<Footer>> groupedFooter = footerList.stream()
                    .collect(Collectors.groupingBy(Footer::getType));
            
            // 构建sections数组
            List<Map<String, Object>> sections = new ArrayList<>();
            
            // 解决方案
            if (groupedFooter.containsKey(1)) {
                Map<String, Object> section = new HashMap<>();
                section.put("title", "解决方案");
                section.put("items", groupedFooter.get(1));
                sections.add(section);
            }
            
            // 新闻资讯
            if (groupedFooter.containsKey(2)) {
                Map<String, Object> section = new HashMap<>();
                section.put("title", "新闻资讯");
                section.put("items", groupedFooter.get(2));
                sections.add(section);
            }
            
            // 关于我们
            if (groupedFooter.containsKey(3)) {
                Map<String, Object> section = new HashMap<>();
                section.put("title", "关于我们");
                section.put("items", groupedFooter.get(3));
                sections.add(section);
            }
            
            // 构建返回数据
            Map<String, Object> result = new HashMap<>();
            result.put("sections", sections);
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取页脚导航数据失败：" + e.getMessage());
        }
    }
} 